# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.

from odoo import api, fields, models, _

class IrConfigParameter(models.Model):
    _inherit = 'ir.config_parameter'



class ResConfigSettings(models.TransientModel):
    _inherit = 'res.config.settings'

    org_id =  fields.Many2one('cncw.org',string='财务机构')
    org_period_id = fields.Many2one('account.fiscalyear',string='会计年度',domain="[('org_id','=',org_id)]")
    cncw_tag_model =  fields.Many2one('cncw.glob_tag_model',string='全局标签')
    cncw_tag_class =  fields.Many2one('cncw.glob_tag_class',string='全局标签')
    has_cncw_accounting_entries = fields.Boolean(default=True)
    has_cncw_chart_of_accounts = fields.Boolean(compute='_compute_has_cncw_chart_of_accounts', string='科目模板？',)
    currency_id = fields.Many2one('res.currency', related="company_id.currency_id",  readonly=True,
        string='货币')
    # Technical field to hide country specific fields from accounting configuration
    country_code = fields.Char(related='company_id.country_id.code', readonly=True)
    cncw_chart_template_id = fields.Many2one('cncw.chart.template', string='财务模板',)
    cncw_sale_tax_id = fields.Many2one('account.tax', string="默认销售税率", related='company_id.account_sale_tax_id', readonly=False)
    cncw_purchase_tax_id = fields.Many2one('account.tax', string="默认采购税率", related='company_id.account_purchase_tax_id', readonly=False)

    module_cncw_ledger = fields.Boolean(string='财务总帐模组')
    module_cncw_statement = fields.Boolean(string='应收应付模组')
    module_customer_credit_limit = fields.Boolean(string="信用额度模组")
    # customer_credit_limit = fields.Boolean(string="信用额度")


    @api.depends('company_id')
    def _compute_has_cncw_chart_of_accounts(self):
        self.has_cncw_chart_of_accounts = bool(self.company_id.cncw_chart_template_id)
        self.has_cncw_accounting_entries = self.env['cncw.chart.template'].existing_accounting(self.company_id)

    def action_add_org(self):
        pass
    #
    # @api.model
    # def get_values(self):
    #     params = self.env['ir.config_parameter'].sudo()
    #     customer_credit_limit = params.get_param('customer_credit_limit',
    #                                              default=False)
    #     self.customer_credit_limit=customer_credit_limit
    #
    # def set_values(self):
    #     if self.group_multi_currency:
    #         self.env.ref('base.group_user').write(
    #             {'implied_ids': [(4, self.env.ref('product.group_sale_pricelist').id)]})
    #     # install a chart of accounts for the given company (if required)
    #     if self.env.company == self.company_id and self.chart_template_id and self.chart_template_id != self.company_id.chart_template_id:
    #         self.chart_template_id._load(15.0, 15.0, self.env.company)
    #
    #     self.env['ir.config_parameter'].sudo().set_param(
    #         "customer_credit_limit",
    #         self.customer_credit_limit)

    # @api.onchange('show_line_subtotals_tax_selection')
    # def _onchange_sale_tax(self):
    #     if self.show_line_subtotals_tax_selection == "tax_excluded":
    #         self.update({
    #             'group_show_line_subtotals_tax_included': False,
    #             'group_show_line_subtotals_tax_excluded': True,
    #         })
    #     else:
    #         self.update({
    #             'group_show_line_subtotals_tax_included': True,
    #             'group_show_line_subtotals_tax_excluded': False,
    #         })
    #
    # @api.onchange('group_analytic_accounting')
    # def onchange_analytic_accounting(self):
    #     if self.group_analytic_accounting:
    #         self.module_account_accountant = True
    #
    # @api.onchange('module_account_budget')
    # def onchange_module_account_budget(self):
    #     if self.module_account_budget:
    #         self.group_analytic_accounting = True
    #
    # @api.onchange('module_account_yodlee')
    # def onchange_account_yodlee(self):
    #     if self.module_account_yodlee:
    #         self.module_account_plaid = True
    #
    # @api.onchange('tax_exigibility')
    # def _onchange_tax_exigibility(self):
    #     res = {}
    #     tax = self.env['account.tax'].search([
    #         ('company_id', '=', self.env.company.id), ('tax_exigibility', '=', 'on_payment')
    #     ], limit=1)
    #     if not self.tax_exigibility and tax:
    #         self.tax_exigibility = True
    #         res['warning'] = {
    #             'title': _('Error!'),
    #             'message': _('You cannot disable this setting because some of your taxes are cash basis. '
    #                          'Modify your taxes first before disabling this setting.')
    #         }
    #     return res
